package com.atguigu.gmall.common.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RabbitService {

    // 引入发送消息的模板
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 封装发送消息的方法.
     * @param exchange
     * @param routingKey
     * @param msg
     * @return
     */
    public Boolean sendMsg(String exchange,String routingKey, Object msg){
        //  发送消息:
        rabbitTemplate.convertAndSend(exchange,routingKey,msg);
        //  默认返回数据
        return true;
    }

    //  发送延迟消息的方法.
    public Boolean sendDelayMsg(String exchange,String routingKey, Object msg,int delayTime){
        //  发送消息:
        this.rabbitTemplate.convertAndSend(exchange, routingKey, msg, message -> {
            //  设置一下延迟时间.
            message.getMessageProperties().setDelay(delayTime*1000);
            return message;
        });
        //  默认返回数据
        return true;
    }

}
